"""
声音播放
"""
import pygame


class SoundPlayer:
    def __init__(self):
        pygame.mixer.init()
        try:
            self.success_sound = pygame.mixer.Sound('higher_C4.mp3')
            self.failed_sound = pygame.mixer.Sound('lower_C4.mp3')
        except pygame.error as e:
            print(f"声音文件加载失败: {e}")
            self.success_sound = None
            self.failed_sound = None

    def play_success(self):
        """播放成功提示音"""
        if self.success_sound:
            self.success_sound.play()
            self._wait_for_sound()

    def play_failed(self):
        """播放失败提示音"""
        if self.failed_sound:
            self.failed_sound.play()
            self._wait_for_sound()

    def _wait_for_sound(self):
        """等待声音播放完成"""
        while pygame.mixer.get_busy():
            pygame.time.Clock().tick(10)